<template>
  <div class="pages brandClass">
    <div class="pageLevel" v-if="!sub">
      <el-button type="primary" size="small" v-if="setRole('新增价格模板')"  @click="clickAdd()">新增价格模板</el-button>
      <div class="tableSearch">
        <el-form size="small">
          <el-form-item label="" label-width="0">
            <el-input clearable v-model="selectdata.keywordStore" placeholder="请输入报货价格模板编号"></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" @click="_handleSearch()">查询</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="tableMain">
        <el-table :data="tableDataStore" stripe style="width: 100%">
          <el-table-column align="center" label="模板协议版本号" prop="templateCode"></el-table-column>
          <el-table-column align="center" label="已分配门店" prop="shopList"></el-table-column>
          <el-table-column align="center" label="操作" width="250">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="clickCheck(scope.row.id,scope.row.shopNum)">查看</el-button>
              <el-button size="small" v-if="setRole('删除')" type="text" @click="_handleDelete(scope.row.id, scope.$index)">删除</el-button>
              <el-button size="small" v-if="setRole('分配到下级门店')" type="text" @click="clickAssign(scope.row.id)">分配到下级门店</el-button>
            </template>
          </el-table-column>
        </el-table>
        <page-unit :pageData="pageDataStore" :func="getStorePriceTemplateListPage"></page-unit>
      </div>

      <el-dialog title="分配到下级门店" :visible.sync="dialogAssign" :closeOnClickModal='false' :closeOnPressEscape='false'>
        <div class="tableSearch">
          <el-form size="mini">
            <el-form-item label="" label-width="0">
              <el-input clearable v-model="selectdata.keywordAssign" placeholder="请输入要分配的门店"></el-input>
            </el-form-item>
            <el-form-item label="" label-width="15px">
              <el-button type="primary" @click="_handleSearchAssign">查询</el-button>
            </el-form-item>
            <el-form-item label="" label-width="15px">
              <el-button type="primary" size="mini" @click="_handleAssign">分配</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table ref="multipleTable" :data="tableDataAssign" @selection-change="_handleSelectionChange" width="100%" :row-key="getRowKeys">
          <el-table-column type="selection" width="55" :reserve-selection="true"></el-table-column>
          <el-table-column align="center" prop="shopCode" label="门店编号"></el-table-column>
          <el-table-column align="center" prop="name" label="门店名称"></el-table-column>
        </el-table>
        <page-unit :pageData="pageDataAssign" :func="selectBaseShopPageList"></page-unit>
      </el-dialog>

    </div>
    <div class="pageLevel" v-else>
      <router-view></router-view>
    </div>
  </div>
</template>
<script>
import pageUnit from "@/components/Page";
import { mapState } from 'vuex'
import util from "@/util";
import {
  delTemplateAndDetail,
  getListPage,
  assignTemplate,
  selectBaseShopPageList
} from "@/api";
export default {
  components: { pageUnit },
  computed: mapState(["sub", "power", "merchantId"]),
  data () {
    return {
      getRowKeys (row) {
        return row.id;
      },
      pageDataStore: {
        pageSize: 20,
        currentPage: 1,
        totalItems: 0,
        pageSizeArr: [5, 10, 20]
      },
      pageDataAssign: {
        pageSize: 20,
        currentPage: 1,
        totalItems: 0,
        pageSizeArr: [5, 10,20]
      },
      selectdata: {
        keywordStore: "",
        keywordAssign: ""
      },
      tableDataStore: [], // 门店报货模板数组
      tableDataAssign: [], // 分配到下级门店数组
      dialogAssign: false, // 分配到下级门店弹窗
      templateId: "", // 模板id
      assignArr: [] // 要分配的门店
    };
  },
  created () { },
  mounted () {
    this.getStorePriceTemplateListPage(20, 1);
  },
  watch: {
    sub: {
      handler: function (val, oldVal) {
        if (oldVal != null && val == false) {
          this.getStorePriceTemplateListPage(20, 1);
        }
      },
      deep: true
    }
  },
  methods: {
               //配置权限
    setRole (key) {
            return util.selectTitle(this.power, key)
        },
    // 获取门店报货价格模板表格数据
    getStorePriceTemplateListPage (pageSize, pageNum) {
      let data = {
        templateCode: this.selectdata.keywordStore,
        pageSize: pageSize,
        pageNum: pageNum,
        shopId: this.merchantId,
        // shopId: 'F56CC6249C000000800000000022E000',
        status: 1,
        dr: 0
      };
      getListPage(data).then(res => {
        this.pageDataStore.totalItems = res.data.pageTotal;
        this.tableDataStore = res.data.list;
      });
    },
    clickAdd () {
      util.routerTo("/operate-management/template-priceTemplate/template-addPriceTempStore");
    },
    _handleDelete (id, index) {
      this.$confirm("确定要删除吗?", "提示", {
        center: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false,
        closeOnPressEscape: false
      })
        .then(() => {
          let formdata = new FormData();
          formdata.append("id", id);
          delTemplateAndDetail(formdata).then(res => {
            if(res.code==200){
             this.getStorePriceTemplateListPage(this.pageDataStore.pageSize, 1);
             this.$message({
              type: "success",
              message: res.msg
             });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 查看
    clickCheck (id,status) {
      util.routerTo("/operate-management/template-priceTemplate/template-editPriceTempStore", {
        id: id,
        status:status
      });
    },
    // 查询
    _handleSearch () {
      this.getStorePriceTemplateListPage(this.pageDataStore.pageSize, 1);
    },

    // 门店-获取列表
    selectBaseShopPageList (pageSize, pageNum) {
      let data = {
        nameOrCode: this.selectdata.keywordAssign,
        pageSize: pageSize,
        pageNum: pageNum,
        businessId: this.merchantId,
        type: 3
      };
      selectBaseShopPageList(data).then(res => {
        this.pageDataAssign.totalItems = Number(res.data.pageTotal);
        this.tableDataAssign = res.data.list;
      });
    },
    // 门店-搜索
    _handleSearchAssign () {
      this.selectBaseShopPageList(this.pageDataAssign.pageSize, 1);
    },
    // 点击分配到下级门店
    clickAssign (id) {
      this.dialogAssign = true;
      this.templateId = id;
      this.selectBaseShopPageList(10, 1);
    },
    _handleSelectionChange (val) {
      this.assignArr = val;
    },
    // 处理分配
    _handleAssign () {
      if (this.assignArr.length == 0) {
        this.$message({
          type: "error",
          message: "请先选择要分配的门店"
        });
      } else {
        let ids = [];
        this.assignArr.forEach((item, index) => {
          ids.push(item.id);
        });
        let data = {
          shopIdList: JSON.stringify(ids),
          templateId: this.templateId
        };
        assignTemplate(data).then(res => {
          this.dialogAssign = false;
          this.getStorePriceTemplateListPage(this.pageDataStore.pageSize, 1);
          if(res.code=="200"){
            this.$message({
            type: "success",
            message: res.msg
           });
          }
        });
      }
    }
  }
};
</script>
<style>
.brandClass {
  position: absolute;
  top: 70px;
  left: 11px;
  right: 11px;
  bottom: 10px;
  /* overflow: auto; */
}
</style>

